﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Gift.DAL.crm
{
    /// <summary>
    /// 数据访问类:t_DALCompany
    /// </summary>
    public partial class t_DALCompany
    {
        public t_DALCompany()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_Company");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Gift.Model.crm.t_Company model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_Company(");
            strSql.Append("Name,Address,Fax,Telephone,Postcode,Email,QQ,Linker,CompanyTypeId,ParentId,LevelId,IsDeleted,CompanyCode,Provence,Area,Mobile,CompanyShortName,CompanyManager,CompanyManagerMobile,Salesman,AddTime,CreditMoney,City)");
            strSql.Append(" values (");
            strSql.Append("@Name,@Address,@Fax,@Telephone,@Postcode,@Email,@QQ,@Linker,@CompanyTypeId,@ParentId,@LevelId,@IsDeleted,@CompanyCode,@Provence,@Area,@Mobile,@CompanyShortName,@CompanyManager,@CompanyManagerMobile,@Salesman,@AddTime,@CreditMoney,@City)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.VarChar,200),
					new SqlParameter("@Address", SqlDbType.VarChar,300),
					new SqlParameter("@Fax", SqlDbType.VarChar,50),
					new SqlParameter("@Telephone", SqlDbType.VarChar,50),
					new SqlParameter("@Postcode", SqlDbType.VarChar,10),
					new SqlParameter("@Email", SqlDbType.VarChar,200),
					new SqlParameter("@QQ", SqlDbType.VarChar,50),
					new SqlParameter("@Linker", SqlDbType.VarChar,100),
					new SqlParameter("@CompanyTypeId", SqlDbType.Int,4),
					new SqlParameter("@ParentId", SqlDbType.Int,4),
					new SqlParameter("@LevelId", SqlDbType.Int,4),
					new SqlParameter("@IsDeleted", SqlDbType.Bit,1),
					new SqlParameter("@CompanyCode", SqlDbType.NVarChar,50),
					new SqlParameter("@Provence", SqlDbType.NVarChar,50),
					new SqlParameter("@Area", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@CompanyShortName", SqlDbType.NVarChar,50),
					new SqlParameter("@CompanyManager", SqlDbType.NVarChar,50),
					new SqlParameter("@CompanyManagerMobile", SqlDbType.NVarChar,50),
					new SqlParameter("@Salesman", SqlDbType.NVarChar,50),
					new SqlParameter("@AddTime", SqlDbType.DateTime),
					new SqlParameter("@CreditMoney", SqlDbType.Decimal,9),
					new SqlParameter("@City", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.Address;
            parameters[2].Value = model.Fax;
            parameters[3].Value = model.Telephone;
            parameters[4].Value = model.Postcode;
            parameters[5].Value = model.Email;
            parameters[6].Value = model.QQ;
            parameters[7].Value = model.Linker;
            parameters[8].Value = model.CompanyTypeId;
            parameters[9].Value = model.ParentId;
            parameters[10].Value = model.LevelId;
            parameters[11].Value = model.IsDeleted;
            parameters[12].Value = model.CompanyCode;
            parameters[13].Value = model.Provence;
            parameters[14].Value = model.Area;
            parameters[15].Value = model.Mobile;
            parameters[16].Value = model.CompanyShortName;
            parameters[17].Value = model.CompanyManager;
            parameters[18].Value = model.CompanyManagerMobile;
            parameters[19].Value = model.Salesman;
            parameters[20].Value = model.AddTime;
            parameters[21].Value = model.CreditMoney;
            parameters[22].Value = model.City;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.crm.t_Company GetModelByAdminId(int AdminId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,Name,Address,Fax,Telephone,Postcode,Email,QQ,Linker,CompanyTypeId,ParentId,LevelId,IsDeleted,CompanyCode,Provence,Area,Mobile,CompanyShortName,CompanyManager,CompanyManagerMobile,Salesman,AddTime,CreditMoney,City from t_Company ");
            strSql.Append(" where Id in(select CompanyId from t_CompanyAdmin where AdminId =" + AdminId + ") ");


            Gift.Model.crm.t_Company model = new Gift.Model.crm.t_Company();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Name"] != null && ds.Tables[0].Rows[0]["Name"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Address"] != null && ds.Tables[0].Rows[0]["Address"].ToString() != "")
                {
                    model.Address = ds.Tables[0].Rows[0]["Address"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Fax"] != null && ds.Tables[0].Rows[0]["Fax"].ToString() != "")
                {
                    model.Fax = ds.Tables[0].Rows[0]["Fax"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Telephone"] != null && ds.Tables[0].Rows[0]["Telephone"].ToString() != "")
                {
                    model.Telephone = ds.Tables[0].Rows[0]["Telephone"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Postcode"] != null && ds.Tables[0].Rows[0]["Postcode"].ToString() != "")
                {
                    model.Postcode = ds.Tables[0].Rows[0]["Postcode"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Email"] != null && ds.Tables[0].Rows[0]["Email"].ToString() != "")
                {
                    model.Email = ds.Tables[0].Rows[0]["Email"].ToString();
                }
                if (ds.Tables[0].Rows[0]["QQ"] != null && ds.Tables[0].Rows[0]["QQ"].ToString() != "")
                {
                    model.QQ = ds.Tables[0].Rows[0]["QQ"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Linker"] != null && ds.Tables[0].Rows[0]["Linker"].ToString() != "")
                {
                    model.Linker = ds.Tables[0].Rows[0]["Linker"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CompanyTypeId"] != null && ds.Tables[0].Rows[0]["CompanyTypeId"].ToString() != "")
                {
                    model.CompanyTypeId = int.Parse(ds.Tables[0].Rows[0]["CompanyTypeId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ParentId"] != null && ds.Tables[0].Rows[0]["ParentId"].ToString() != "")
                {
                    model.ParentId = int.Parse(ds.Tables[0].Rows[0]["ParentId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LevelId"] != null && ds.Tables[0].Rows[0]["LevelId"].ToString() != "")
                {
                    model.LevelId = int.Parse(ds.Tables[0].Rows[0]["LevelId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IsDeleted"] != null && ds.Tables[0].Rows[0]["IsDeleted"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsDeleted"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsDeleted"].ToString().ToLower() == "true"))
                    {
                        model.IsDeleted = true;
                    }
                    else
                    {
                        model.IsDeleted = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["CompanyCode"] != null && ds.Tables[0].Rows[0]["CompanyCode"].ToString() != "")
                {
                    model.CompanyCode = ds.Tables[0].Rows[0]["CompanyCode"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Provence"] != null && ds.Tables[0].Rows[0]["Provence"].ToString() != "")
                {
                    model.Provence = ds.Tables[0].Rows[0]["Provence"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Area"] != null && ds.Tables[0].Rows[0]["Area"].ToString() != "")
                {
                    model.Area = ds.Tables[0].Rows[0]["Area"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Mobile"] != null && ds.Tables[0].Rows[0]["Mobile"].ToString() != "")
                {
                    model.Mobile = ds.Tables[0].Rows[0]["Mobile"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CompanyShortName"] != null && ds.Tables[0].Rows[0]["CompanyShortName"].ToString() != "")
                {
                    model.CompanyShortName = ds.Tables[0].Rows[0]["CompanyShortName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CompanyManager"] != null && ds.Tables[0].Rows[0]["CompanyManager"].ToString() != "")
                {
                    model.CompanyManager = ds.Tables[0].Rows[0]["CompanyManager"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CompanyManagerMobile"] != null && ds.Tables[0].Rows[0]["CompanyManagerMobile"].ToString() != "")
                {
                    model.CompanyManagerMobile = ds.Tables[0].Rows[0]["CompanyManagerMobile"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Salesman"] != null && ds.Tables[0].Rows[0]["Salesman"].ToString() != "")
                {
                    model.Salesman = ds.Tables[0].Rows[0]["Salesman"].ToString();
                }
                if (ds.Tables[0].Rows[0]["AddTime"] != null && ds.Tables[0].Rows[0]["AddTime"].ToString() != "")
                {
                    model.AddTime = DateTime.Parse(ds.Tables[0].Rows[0]["AddTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CreditMoney"] != null && ds.Tables[0].Rows[0]["CreditMoney"].ToString() != "")
                {
                    model.CreditMoney = decimal.Parse(ds.Tables[0].Rows[0]["CreditMoney"].ToString());
                }
                if (ds.Tables[0].Rows[0]["City"] != null && ds.Tables[0].Rows[0]["City"].ToString() != "")
                {
                    model.City = ds.Tables[0].Rows[0]["City"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gift.Model.crm.t_Company model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_Company set ");
            strSql.Append("Name=@Name,");
            strSql.Append("Address=@Address,");
            strSql.Append("Fax=@Fax,");
            strSql.Append("Telephone=@Telephone,");
            strSql.Append("Postcode=@Postcode,");
            strSql.Append("Email=@Email,");
            strSql.Append("QQ=@QQ,");
            strSql.Append("Linker=@Linker,");
            strSql.Append("CompanyTypeId=@CompanyTypeId,");
            strSql.Append("ParentId=@ParentId,");
            strSql.Append("LevelId=@LevelId,");
            strSql.Append("IsDeleted=@IsDeleted,");
            strSql.Append("CompanyCode=@CompanyCode,");
            strSql.Append("Provence=@Provence,");
            strSql.Append("Area=@Area,");
            strSql.Append("Mobile=@Mobile,");
            strSql.Append("CompanyShortName=@CompanyShortName,");
            strSql.Append("CompanyManager=@CompanyManager,");
            strSql.Append("CompanyManagerMobile=@CompanyManagerMobile,");
            strSql.Append("Salesman=@Salesman,");
            strSql.Append("AddTime=@AddTime,");
            strSql.Append("CreditMoney=@CreditMoney,");
            strSql.Append("City=@City");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.VarChar,200),
					new SqlParameter("@Address", SqlDbType.VarChar,300),
					new SqlParameter("@Fax", SqlDbType.VarChar,50),
					new SqlParameter("@Telephone", SqlDbType.VarChar,50),
					new SqlParameter("@Postcode", SqlDbType.VarChar,10),
					new SqlParameter("@Email", SqlDbType.VarChar,200),
					new SqlParameter("@QQ", SqlDbType.VarChar,50),
					new SqlParameter("@Linker", SqlDbType.VarChar,100),
					new SqlParameter("@CompanyTypeId", SqlDbType.Int,4),
					new SqlParameter("@ParentId", SqlDbType.Int,4),
					new SqlParameter("@LevelId", SqlDbType.Int,4),
					new SqlParameter("@IsDeleted", SqlDbType.Bit,1),
					new SqlParameter("@CompanyCode", SqlDbType.NVarChar,50),
					new SqlParameter("@Provence", SqlDbType.NVarChar,50),
					new SqlParameter("@Area", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@CompanyShortName", SqlDbType.NVarChar,50),
					new SqlParameter("@CompanyManager", SqlDbType.NVarChar,50),
					new SqlParameter("@CompanyManagerMobile", SqlDbType.NVarChar,50),
					new SqlParameter("@Salesman", SqlDbType.NVarChar,50),
					new SqlParameter("@AddTime", SqlDbType.DateTime),
					new SqlParameter("@CreditMoney", SqlDbType.Decimal,9),
					new SqlParameter("@City", SqlDbType.NVarChar,50),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.Address;
            parameters[2].Value = model.Fax;
            parameters[3].Value = model.Telephone;
            parameters[4].Value = model.Postcode;
            parameters[5].Value = model.Email;
            parameters[6].Value = model.QQ;
            parameters[7].Value = model.Linker;
            parameters[8].Value = model.CompanyTypeId;
            parameters[9].Value = model.ParentId;
            parameters[10].Value = model.LevelId;
            parameters[11].Value = model.IsDeleted;
            parameters[12].Value = model.CompanyCode;
            parameters[13].Value = model.Provence;
            parameters[14].Value = model.Area;
            parameters[15].Value = model.Mobile;
            parameters[16].Value = model.CompanyShortName;
            parameters[17].Value = model.CompanyManager;
            parameters[18].Value = model.CompanyManagerMobile;
            parameters[19].Value = model.Salesman;
            parameters[20].Value = model.AddTime;
            parameters[21].Value = model.CreditMoney;
            parameters[22].Value = model.City;
            parameters[23].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update from t_Company set isDeleted=1 ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_Company ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.crm.t_Company GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,Name,Address,Fax,Telephone,Postcode,Email,QQ,Linker,CompanyTypeId,ParentId,LevelId,IsDeleted,CompanyCode,Provence,Area,Mobile,CompanyShortName,CompanyManager,CompanyManagerMobile,Salesman,AddTime,CreditMoney,City from t_Company ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            Gift.Model.crm.t_Company model = new Gift.Model.crm.t_Company();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Name"] != null && ds.Tables[0].Rows[0]["Name"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Address"] != null && ds.Tables[0].Rows[0]["Address"].ToString() != "")
                {
                    model.Address = ds.Tables[0].Rows[0]["Address"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Fax"] != null && ds.Tables[0].Rows[0]["Fax"].ToString() != "")
                {
                    model.Fax = ds.Tables[0].Rows[0]["Fax"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Telephone"] != null && ds.Tables[0].Rows[0]["Telephone"].ToString() != "")
                {
                    model.Telephone = ds.Tables[0].Rows[0]["Telephone"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Postcode"] != null && ds.Tables[0].Rows[0]["Postcode"].ToString() != "")
                {
                    model.Postcode = ds.Tables[0].Rows[0]["Postcode"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Email"] != null && ds.Tables[0].Rows[0]["Email"].ToString() != "")
                {
                    model.Email = ds.Tables[0].Rows[0]["Email"].ToString();
                }
                if (ds.Tables[0].Rows[0]["QQ"] != null && ds.Tables[0].Rows[0]["QQ"].ToString() != "")
                {
                    model.QQ = ds.Tables[0].Rows[0]["QQ"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Linker"] != null && ds.Tables[0].Rows[0]["Linker"].ToString() != "")
                {
                    model.Linker = ds.Tables[0].Rows[0]["Linker"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CompanyTypeId"] != null && ds.Tables[0].Rows[0]["CompanyTypeId"].ToString() != "")
                {
                    model.CompanyTypeId = int.Parse(ds.Tables[0].Rows[0]["CompanyTypeId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ParentId"] != null && ds.Tables[0].Rows[0]["ParentId"].ToString() != "")
                {
                    model.ParentId = int.Parse(ds.Tables[0].Rows[0]["ParentId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LevelId"] != null && ds.Tables[0].Rows[0]["LevelId"].ToString() != "")
                {
                    model.LevelId = int.Parse(ds.Tables[0].Rows[0]["LevelId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IsDeleted"] != null && ds.Tables[0].Rows[0]["IsDeleted"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsDeleted"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsDeleted"].ToString().ToLower() == "true"))
                    {
                        model.IsDeleted = true;
                    }
                    else
                    {
                        model.IsDeleted = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["CompanyCode"] != null && ds.Tables[0].Rows[0]["CompanyCode"].ToString() != "")
                {
                    model.CompanyCode = ds.Tables[0].Rows[0]["CompanyCode"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Provence"] != null && ds.Tables[0].Rows[0]["Provence"].ToString() != "")
                {
                    model.Provence = ds.Tables[0].Rows[0]["Provence"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Area"] != null && ds.Tables[0].Rows[0]["Area"].ToString() != "")
                {
                    model.Area = ds.Tables[0].Rows[0]["Area"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Mobile"] != null && ds.Tables[0].Rows[0]["Mobile"].ToString() != "")
                {
                    model.Mobile = ds.Tables[0].Rows[0]["Mobile"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CompanyShortName"] != null && ds.Tables[0].Rows[0]["CompanyShortName"].ToString() != "")
                {
                    model.CompanyShortName = ds.Tables[0].Rows[0]["CompanyShortName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CompanyManager"] != null && ds.Tables[0].Rows[0]["CompanyManager"].ToString() != "")
                {
                    model.CompanyManager = ds.Tables[0].Rows[0]["CompanyManager"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CompanyManagerMobile"] != null && ds.Tables[0].Rows[0]["CompanyManagerMobile"].ToString() != "")
                {
                    model.CompanyManagerMobile = ds.Tables[0].Rows[0]["CompanyManagerMobile"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Salesman"] != null && ds.Tables[0].Rows[0]["Salesman"].ToString() != "")
                {
                    model.Salesman = ds.Tables[0].Rows[0]["Salesman"].ToString();
                }
                if (ds.Tables[0].Rows[0]["AddTime"] != null && ds.Tables[0].Rows[0]["AddTime"].ToString() != "")
                {
                    model.AddTime = DateTime.Parse(ds.Tables[0].Rows[0]["AddTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CreditMoney"] != null && ds.Tables[0].Rows[0]["CreditMoney"].ToString() != "")
                {
                    model.CreditMoney = decimal.Parse(ds.Tables[0].Rows[0]["CreditMoney"].ToString());
                }
                if (ds.Tables[0].Rows[0]["City"] != null && ds.Tables[0].Rows[0]["City"].ToString() != "")
                {
                    model.City = ds.Tables[0].Rows[0]["City"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.crm.t_Company GetModelByName(string companyName)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,Name,Address,Fax,Telephone,Postcode,Email,QQ,Linker,CompanyTypeId,ParentId,LevelId,IsDeleted,CompanyCode,Provence,Area,Mobile,CompanyShortName,CompanyManager,CompanyManagerMobile,Salesman,AddTime,CreditMoney,City from t_Company ");
            strSql.Append(" where Name=@companyName");
            SqlParameter[] parameters = {
					new SqlParameter("@companyName", SqlDbType.NVarChar,300)
			};
            parameters[0].Value = companyName;

            Gift.Model.crm.t_Company model = new Gift.Model.crm.t_Company();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Name"] != null && ds.Tables[0].Rows[0]["Name"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Address"] != null && ds.Tables[0].Rows[0]["Address"].ToString() != "")
                {
                    model.Address = ds.Tables[0].Rows[0]["Address"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Fax"] != null && ds.Tables[0].Rows[0]["Fax"].ToString() != "")
                {
                    model.Fax = ds.Tables[0].Rows[0]["Fax"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Telephone"] != null && ds.Tables[0].Rows[0]["Telephone"].ToString() != "")
                {
                    model.Telephone = ds.Tables[0].Rows[0]["Telephone"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Postcode"] != null && ds.Tables[0].Rows[0]["Postcode"].ToString() != "")
                {
                    model.Postcode = ds.Tables[0].Rows[0]["Postcode"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Email"] != null && ds.Tables[0].Rows[0]["Email"].ToString() != "")
                {
                    model.Email = ds.Tables[0].Rows[0]["Email"].ToString();
                }
                if (ds.Tables[0].Rows[0]["QQ"] != null && ds.Tables[0].Rows[0]["QQ"].ToString() != "")
                {
                    model.QQ = ds.Tables[0].Rows[0]["QQ"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Linker"] != null && ds.Tables[0].Rows[0]["Linker"].ToString() != "")
                {
                    model.Linker = ds.Tables[0].Rows[0]["Linker"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CompanyTypeId"] != null && ds.Tables[0].Rows[0]["CompanyTypeId"].ToString() != "")
                {
                    model.CompanyTypeId = int.Parse(ds.Tables[0].Rows[0]["CompanyTypeId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ParentId"] != null && ds.Tables[0].Rows[0]["ParentId"].ToString() != "")
                {
                    model.ParentId = int.Parse(ds.Tables[0].Rows[0]["ParentId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LevelId"] != null && ds.Tables[0].Rows[0]["LevelId"].ToString() != "")
                {
                    model.LevelId = int.Parse(ds.Tables[0].Rows[0]["LevelId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IsDeleted"] != null && ds.Tables[0].Rows[0]["IsDeleted"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsDeleted"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsDeleted"].ToString().ToLower() == "true"))
                    {
                        model.IsDeleted = true;
                    }
                    else
                    {
                        model.IsDeleted = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["CompanyCode"] != null && ds.Tables[0].Rows[0]["CompanyCode"].ToString() != "")
                {
                    model.CompanyCode = ds.Tables[0].Rows[0]["CompanyCode"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Provence"] != null && ds.Tables[0].Rows[0]["Provence"].ToString() != "")
                {
                    model.Provence = ds.Tables[0].Rows[0]["Provence"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Area"] != null && ds.Tables[0].Rows[0]["Area"].ToString() != "")
                {
                    model.Area = ds.Tables[0].Rows[0]["Area"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Mobile"] != null && ds.Tables[0].Rows[0]["Mobile"].ToString() != "")
                {
                    model.Mobile = ds.Tables[0].Rows[0]["Mobile"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CompanyShortName"] != null && ds.Tables[0].Rows[0]["CompanyShortName"].ToString() != "")
                {
                    model.CompanyShortName = ds.Tables[0].Rows[0]["CompanyShortName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CompanyManager"] != null && ds.Tables[0].Rows[0]["CompanyManager"].ToString() != "")
                {
                    model.CompanyManager = ds.Tables[0].Rows[0]["CompanyManager"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CompanyManagerMobile"] != null && ds.Tables[0].Rows[0]["CompanyManagerMobile"].ToString() != "")
                {
                    model.CompanyManagerMobile = ds.Tables[0].Rows[0]["CompanyManagerMobile"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Salesman"] != null && ds.Tables[0].Rows[0]["Salesman"].ToString() != "")
                {
                    model.Salesman = ds.Tables[0].Rows[0]["Salesman"].ToString();
                }
                if (ds.Tables[0].Rows[0]["AddTime"] != null && ds.Tables[0].Rows[0]["AddTime"].ToString() != "")
                {
                    model.AddTime = DateTime.Parse(ds.Tables[0].Rows[0]["AddTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CreditMoney"] != null && ds.Tables[0].Rows[0]["CreditMoney"].ToString() != "")
                {
                    model.CreditMoney = decimal.Parse(ds.Tables[0].Rows[0]["CreditMoney"].ToString());
                }
                if (ds.Tables[0].Rows[0]["City"] != null && ds.Tables[0].Rows[0]["City"].ToString() != "")
                {
                    model.City = ds.Tables[0].Rows[0]["City"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        public bool IsExistsOnSale(string companyId, float oSale)
        {
            StringBuilder strSql = new StringBuilder();
            string strSql2 = string.Empty;
            strSql.Append("select top 1 OnSale from t_CompanyOnSale where CompanyId=" + companyId + " order by AddDate Desc ");
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (oSale == float.Parse(ds.Tables[0].Rows[0]["OnSale"].ToString()))
                {
                    return true;
                }
                strSql2 = "insert into t_CompanyOnSale(CompanyId, OnSale, AddDate) values(" + companyId + "," + oSale.ToString() + ",'" + DateTime.Now.ToString() + "')";
                DbHelperSQL.ExecuteSql(strSql2);
                return false;

            }
            else
            {
                strSql2 = "insert into t_CompanyOnSale(CompanyId, OnSale, AddDate) values(" + companyId + "," + oSale.ToString() + ",'" + DateTime.Now.ToString() + "')";
                DbHelperSQL.ExecuteSql(strSql2);
                return false;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetListForCompanyLevel()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,LevelName From t_CompanyLevel ");
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataSet GetProvenceList(string sWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select distinct provence From t_Company where isnull(provence,'') != '' ");
            strSql.Append(sWhere);

            strSql.Append(" order by provence asc ");
            return DbHelperSQL.Query(strSql.ToString());
        }
        public int DeleteMany(string sIds)
        {
            string sSQL;
            if (!string.IsNullOrEmpty(sIds))
            {
                sSQL = "Update t_Company set IsDeleted=1   WHERE Id in ( " + sIds + ")";
            }
            else
            {
                sSQL = "Update tbCompany set IsDeleted=1 ";
            }
            return DbHelperSQL.ExecuteSql(sSQL);
        }


        public bool ExistsByShortName(string companyShortName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_Company");
            strSql.Append(" where CompanyShortName='" + companyShortName + "' ");
            return DbHelperSQL.Exists(strSql.ToString());
        }



        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,Name,Address,Fax,Telephone,Postcode,Email,QQ,Linker,CompanyTypeId,ParentId,LevelId,IsDeleted,CompanyCode,Provence,Area,Mobile,CompanyShortName,CompanyManager,CompanyManagerMobile,Salesman,AddTime,CreditMoney,City ");
            strSql.Append(" FROM t_Company ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by Name ");
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,Name,Address,Fax,Telephone,Postcode,Email,QQ,Linker,CompanyTypeId,ParentId,LevelId,IsDeleted,CompanyCode,Provence,Area,Mobile,CompanyShortName,CompanyManager,CompanyManagerMobile,Salesman,AddTime,CreditMoney,City ");
            strSql.Append(" FROM t_Company ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM t_Company ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Id desc");
            }
            strSql.Append(")AS Row, T.*  from t_Company T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "t_Company";
            parameters[1].Value = "Id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        public decimal GetCompanyOnSale(string companyId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 OnSale from t_CompanyOnSale where CompanyId=" + companyId + " order by AddDate Desc ");
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return decimal.Parse(ds.Tables[0].Rows[0]["OnSale"].ToString());
            }
            else
            {
                return 1;
            }
        }


        public int GetGenerateCompanyCode()
        {
            StringBuilder strSql = new StringBuilder();
            int iComanyCode = 0;


                strSql.Append("select isnull(max(CompanyCode),0) as CompanyCode From tbCompany where isnumeric(CompanyCode) = 1");
        
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
              
                    if (ds.Tables[0].Rows[0][0].ToString() == "0" || ds.Tables[0].Rows[0][0].ToString().Length == 3)
                    {
                        iComanyCode = 1001;
                    }
                    else
                    {
                        iComanyCode = int.Parse(ds.Tables[0].Rows[0][0].ToString()) + 1;
                    }
            }
            return iComanyCode;
        }



        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistsByCode(string companyCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_Company");
            strSql.Append(" where CompanyCode='" + companyCode + "' ");
            return DbHelperSQL.Exists(strSql.ToString());
        }

        #endregion  Method
    }
}

